//
//  HLMatchNightTipView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/6.
//

#import "HLMatchNightTipView.h"
#import <YYText/YYText.h>
#import "HLMatchNightTimeView.h"
#import "HLMatchRequest.h"
#import "HLMatchFateViewController.h"

@interface HLMatchNightTipView ()

@property (nonatomic, strong) UIImageView *backView;
@property (nonatomic, strong) YYLabel *titleLabel;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *timeTipLabel;

@property (nonatomic, strong) HLMatchNightTimeView *minView;
@property (nonatomic, strong) HLMatchNightTimeView *hourView;
@property (nonatomic, strong) HLMatchNightTimeView *secondView;

@property (nonatomic, strong) QMUIButton *okButton;

@end

@implementation HLMatchNightTipView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    
    self.backView = [[UIImageView alloc] initWithImage:UIImageMake(@"match_night")];
    [self addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    
    self.titleLabel = [[YYLabel alloc] init];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(58);
        make.top.mas_equalTo(140);
    }];
    
    NSString *text = @"嗨，尊敬的用户";;
    NSMutableAttributedString *attString = [NSMutableAttributedString.alloc initWithString:text];
    [attString yy_setColor:HLTextColor1 range:NSMakeRange(0, attString.length)];
    [attString yy_setFont:UIFontMake(15) range:NSMakeRange(0, attString.length)];
    [attString yy_setTextHighlightRange:[text rangeOfString:@"尊敬的用户"] color:[UIColor qmui_colorWithHexString:@"#2D72D0"] backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
    }];
    _titleLabel.attributedText = attString;
    
    
    self.tipLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(15) textColor:HLTextColor1];
    _tipLabel.text = @"夜间匹配功能在每天的固定时间";
    [self addSubview:_tipLabel];
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel);
        make.top.equalTo(_titleLabel.mas_bottom).offset(26);
    }];
    
    self.timeLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(15) textColor:HLTextColor1];
    _timeLabel.text = @"19：00至次日凌晨5：00\n此时间段为您开放，敬请期待！";
    _timeLabel.numberOfLines = 2;
    _timeLabel.qmui_lineHeight = 20;
    [self addSubview:_timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_tipLabel);
        make.top.equalTo(_tipLabel.mas_bottom).offset(26);
    }];
    
    self.timeTipLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(12) textColor:[HLTextColor1 colorWithAlphaComponent:0.6]];
    _timeTipLabel.text = @"距离开启夜间模式还剩";
    [self addSubview:_timeTipLabel];
    [_timeTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_timeLabel);
        make.top.equalTo(_timeLabel.mas_bottom).offset(12);
    }];
    
    
    self.minView = [[HLMatchNightTimeView alloc] init];
    _minView.numLabel.text = @"05";
    _minView.unitLabel.text = @"分";
    [self addSubview:_minView];
    [_minView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_timeTipLabel.mas_bottom).offset(6);
    }];
    
    self.hourView = [[HLMatchNightTimeView alloc] init];
    _hourView.numLabel.text = @"00";
    _hourView.unitLabel.text = @"时";
    [self addSubview:_hourView];
    [_hourView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_minView);
        make.right.equalTo(_minView.mas_left).offset(-8);
    }];
    
    
    self.secondView = [[HLMatchNightTimeView alloc] init];
    _secondView.numLabel.text = @"25";
    _secondView.unitLabel.text = @"秒";
    [self addSubview:_secondView];
    [_secondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_minView);
        make.left.equalTo(_minView.mas_right).offset(8);
    }];
    
    
    
    self.okButton = [[QMUIButton alloc] init];
    [_okButton setImage:UIImageMake(@"match_night_ok") forState:UIControlStateNormal];
    [self addSubview:_okButton];
    [_okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-16);
    }];
    
}

+ (void)show
{
    CGFloat height = 435;
    HLMatchNightTipView *containerView = [[HLMatchNightTipView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    
    QMUIModalPresentationViewController *modalViewController = [[QMUIModalPresentationViewController alloc] init];

    modalViewController.contentView = containerView;
    
    [containerView.okButton setQmui_tapBlock:^(__kindof UIControl *sender) {
        [modalViewController hideWithAnimated:YES completion:nil];
    }];
    
    HLMatchRequest *nightRequest = [[HLMatchRequest alloc] initWithType:HLMatchTypeNight params:@{}];
    [nightRequest startWithCompletionBlockWithSuccess:^(__kindof HLMatchRequest * _Nonnull request) {
        if (request.isSuccess){
            HLMatchNightModel *night = [request night];
            if ([night.duration integerValue] >
                0){
                containerView.timeLabel.text = [NSString stringWithFormat:@"%@至次日凌晨%@\n此时间段为您开放，敬请期待！", night.startTime, night.endTime];
                
                NSInteger timerCount = [night.duration integerValue];
                
                NSInteger seconds = timerCount % 60;
                NSInteger minutes = (timerCount / 60) % 60;
                NSInteger hours = (timerCount / 60 / 60) % 60;
                
                containerView.hourView.numLabel.text = [NSString stringWithFormat:@"%ld", hours];
                containerView.minView.numLabel.text = [NSString stringWithFormat:@"%ld", minutes];
                containerView.secondView.numLabel.text = [NSString stringWithFormat:@"%ld", seconds];
                [modalViewController showWithAnimated:YES completion:nil];
            } else {
                HLMatchFateViewController *matchFate = [[HLMatchFateViewController alloc] init];
                [[QMUIHelper visibleViewController].navigationController pushViewController:matchFate animated:YES];
            }
            
            
        }
    } failure:nil];

}

@end
